package cc.telecomdigital.tdstock.trading.Services;

import java.io.IOException;

import cc.telecomdigital.tdstock.R;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Services.CommunicTools;
import cc.telecomdigital.tdstock.trading.Common.Trade_StockViewInfoItem;
import cc.telecomdigital.tdstock.trading.Tools.Trade_StockFunctionTools;
import android.content.Context;

//Get Less Information
public class TradeGetStockQuote extends TradeGetDetailStockQuote
{
	private final String	LOG_TAG	= "TradeGetStockQuote";

	public TradeGetStockQuote(Context iContext)
	{
		super(iContext);
	}
	
	/** Place in the quote number that you are looking for **/
	public void SetRequestQuoteInfo(boolean iIsDevelopment, String iQuoteNumber, String iLogonAccount, String iLogonToken)
	{
		if ((iQuoteNumber == null) || (iLogonAccount == null) || (iLogonToken == null))
			urlString = null;
		else
			urlString = TradeAppInfo.GetStockCodeURL(iIsDevelopment, iQuoteNumber, iLogonAccount, iLogonToken);
		
		urlPort = TradeAppInfo.GetStockCodePort();
	}

	@Override
	protected TradeGetStockQuoteResult SendDataSub(HttpClient iClient, HttpGet iRequest)
	{
		String 	 stockCode, chnName, engName, nominal;
		String 	 bidPrice, askPrice, lotSize;
		String 	 currencyCode;
		String     prevClosePrice;
		String     statusMsg, descriptMessage;
		String     tmpString;
		Trade_StockViewInfoItem stockInfo;

		stockInfo  = null;
		
		statusMsg = "";
		descriptMessage = "";
		stockCode = "";
		chnName = ""; 
		engName = "";
		nominal = "";
		bidPrice = "";
		askPrice 	   = "";
		lotSize      = "";
		currencyCode = "";	
		prevClosePrice = "";
//		isSuspended   = false;
		try
		{
			HttpResponse webResponse;
			StringBuffer contentString;					

			webResponse = iClient.execute(iRequest);
			
			contentString = CommunicTools.GetWebResponseInStringBuffer(webResponse);										
			
			statusMsg = CommunicTools.FindXMLStringContent(contentString, "Status");
			descriptMessage = CommunicTools.FindXMLStringContent(contentString, "ErrorMessage");
			stockCode = CommunicTools.FindXMLStringContent(contentString, "STOCK_CODE");
			chnName = CommunicTools.FindXMLStringContent(contentString, "SCHI_NAME");
			chnName = chnName.trim();
			engName = CommunicTools.FindXMLStringContent(contentString, "SENG_NAME");
			engName = engName.trim();
			nominal = CommunicTools.FindXMLStringContent(contentString, "NOMINAL");
			bidPrice = CommunicTools.FindXMLStringContent(contentString, "BID_PRICE");
			askPrice = CommunicTools.FindXMLStringContent(contentString, "ASK_PRICE");
			lotSize = CommunicTools.FindXMLStringContent(contentString, "LOT_SIZE");
//			netChange = CommunicTools.FindXMLStringContent(contentString, "NET");
			currencyCode = CommunicTools.FindXMLStringContent(contentString, "CURRENCY_CODE");
			prevClosePrice  = CommunicTools.FindXMLStringContent(contentString, "PREV_CLOSE_PRICE");
			
//			tmpString = CommunicTools.FindXMLStringContent(contentString, "SUSP_STOCK");
//			if (tmpString.equals("Y"))
//				isSuspended = true;
			
			tmpString = stockCode;
			if (tmpString.equals("") == false)
				tmpString = Trade_StockFunctionTools.FormatFullStockCode(tmpString);
			
			stockInfo = new Trade_StockViewInfoItem(tmpString, chnName, engName);
			stockInfo.SetStartPrice(prevClosePrice, false);
			stockInfo.SetPriceInfo(nominal, bidPrice, askPrice, true);
			
			stockInfo.SetStepVolume(lotSize);								
			stockInfo.SetCurrencyCode(currencyCode);			
			//Calculate the spread price
			//Not guarantee to have this
			if (nominal.equals("") == false)
			{
				tmpString = Trade_StockFunctionTools.GetStockPriceSpread(stockInfo.GetCurPriceText());
				stockInfo.SetSpreadPrice(tmpString);	
			}

			//Custom set the error to -2 not to conflict with the -1 (Session Expired)			
			if (statusMsg.equals(""))
			{
				if (stockCode.equals(""))
				{
					statusMsg = "-2";
					if (descriptMessage.equals(""))
						descriptMessage = callerContext.getResources().getString(R.string.StockNotFound_Text);
				}
				else
					statusMsg = "0";
			}
			
			if (debugMode)
				TDStockLog.i(LOG_TAG, "SendDataSub: Status=" + statusMsg);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();			
			TDStockLog.e(LOG_TAG, "SendDataSub=>ClientProtocolException: " + e.toString());
			return null;
		}
		catch (IOException e)
		{
			e.printStackTrace();			
			TDStockLog.e(LOG_TAG, "SendDataSub=>IOException: " + e.toString());
            return null;			
		}
		catch (Exception e)
		{
			e.printStackTrace();			
			TDStockLog.e(LOG_TAG, "SendDataSub=>Exception: " + e.toString());
			return null;
		}
		
		return new TradeGetStockQuoteResult(statusMsg, descriptMessage, stockInfo); 		
	}	

}
